package org.huang.user.service;

import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.huang.exception.BizException;
import org.huang.user.api.UserService;
import org.huang.user.dao.UserMapper;
import org.huang.user.entity.User;
import org.huang.user.exception.UserErrorStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @描述：UserServiceImpl类描述
 * @作者: huanganwei
 * @创建时间: 2018-08-08 18:01:37
 * @版本: 1.0
 */
@Slf4j
@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    @Transactional
    public boolean addUser(User user) {
        try{
            userMapper.insertSelective(user);
        }catch (Exception e){
            log.error("添加用户信息失败"+e.getMessage());
            return false;
        }

        return true;
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }



    @Override
    public User findUserByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public boolean login(String username, String password) {
        User user = userMapper.selectByUsername(username);
        if(user == null)
            throw new BizException(UserErrorStatus.ERROR_USERNAME);
        else if(!password.equals(user.getPassword()))
            throw new BizException(UserErrorStatus.ERROR_PASSWORD);
        return true;
    }
}
